package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.Calendar;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.FormularDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.FormularTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.FormularServiceInt;

/**
 * Session Bean implementation class FormularE112Service
 * 
 * @author Patrick kendzo Email: pkendzo@msn.com
 */

@Stateless
@Remote({ FormularServiceInt.class })
public class FormularService implements FormularServiceInt {

	@SuppressWarnings("unused")
	private FormularTBL tmpForm;
	@EJB
	private FormularDAOInt formDao;

	@EJB
	private CustomerServiceInt cust;

	/**
	 * Default constructor.
	 */
	public FormularService() {
		tmpForm = new FormularTBL();
	}

	@Override
	public int deleteFormular(Long id) {
		this.formDao.delete(id);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getA_instance(FormularTBL instance) {
		this.tmpForm = instance;

	}

	@Override
	public int createFormular(String matricule, Calendar datum,
			String dateiName, String bezeichner, String url,
			String actualFomularNumber, int typ) {
		FormularTBL tmpForm_1 = new FormularTBL();

		tmpForm_1.setCustomer(cust.getCustomer(matricule));
		tmpForm_1.setDatum(datum);
		tmpForm_1.setDateiName(dateiName);
		tmpForm_1.setBezeichner(bezeichner);
		tmpForm_1.setSpeicherOrt(url);
		tmpForm_1.setTyp(typ);

		this.formDao.persist(tmpForm_1);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public FormularTBL[] getFormularBYCustomer(String matricule) {
		Object[] tmp = formDao.retrieveFormularItemsByCustomer(matricule)
				.toArray();
		FormularTBL[] eintraege = new FormularTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (FormularTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public FormularTBL[] getFormularBY_Typ(String typ) {
		Object[] tmp = formDao.retrieveFormularBY_Typ(typ).toArray();
		FormularTBL[] eintraege = new FormularTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (FormularTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public FormularTBL[] getFormular_BYCustomer_and_BYtyp(String matricule,
			String typ) {
		Object[] tmp = formDao.retrieveFormular_BYCustomer_and_BYtyp(matricule,
				typ).toArray();
		FormularTBL[] eintraege = new FormularTBL[tmp.length];

		for (int i = 0; i < tmp.length; i++) {
			eintraege[i] = (FormularTBL) tmp[i];
		}
		return eintraege;
	}

	@Override
	public Long getSubPrimaryKeyIndexBYTyp(int typ) {
		return formDao.findSubPrimaryKeyIndexBYTyp(typ);
	}

}
